<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="small" @click="handleExport"
                   v-hasPermi="['channel:countPvInfo:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-search" size="small" @click="handle"
                   v-hasPermi="['channel:countPvInfo:count']">统计图</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="countPvInfoList" @selection-change="handleSelectionChange">
      <el-table-column label="渠道编号" align="center" prop="channelCode" />
      <el-table-column label="渠道名称" align="center" prop="channelName" />
      <el-table-column label="应用编号" align="center" prop="appCode" />
      <el-table-column label="应用名称" align="center" prop="appName" />
      <el-table-column label="IP" align="center" prop="reqIp" />
      <el-table-column label="浏览器名称" align="center" prop="browserName" />
      <el-table-column label="平台名称" align="center" prop="platformName" />
      <el-table-column label="创建时间" align="center" width="150">
        <template slot-scope="scope">
          {{formatTimestamp(scope.row.createTime)}}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <Count ref="CountRef" />
  </div>
</template>

<script>
import { listCountPvInfo, getCountPvInfo, delCountPvInfo, addCountPvInfo, updateCountPvInfo } from "@/api/channel/countPvInfo";
import Count from "@/views/channel/countPvInfo/echars/count.vue";

export default {
  name: "CountPvInfo",
  components: {Count},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 渠道统计pv信息表格数据
      countPvInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        randomId: null,
        reqIp: null,
        browserName: null,
        platformName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dateInfo: [
          { required: true, message: "日期不能为空", trigger: "blur" }
        ],
        channelCode: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" }
        ],
        channelName: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        appCode: [
          { required: true, message: "应用编号不能为空", trigger: "blur" }
        ],
        appName: [
          { required: true, message: "应用名称不能为空", trigger: "blur" }
        ],
        randomId: [
          { required: true, message: "pv随机ID不能为空", trigger: "blur" }
        ],
        reqIp: [
          { required: true, message: "pvip不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.$route.query
    }
    this.getList();
  },
  watch:{
    $route(){
      this.getList()
    }
  },
  methods: {
    formatTimestamp(timestamp) {
      if (!timestamp) {
        return '';
      }
      const date = new Date(timestamp);
      const formattedDate = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`;
      return formattedDate;
    },
    /** 查询渠道统计pv信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.channelCode = this.$route.query.channelCode;
      this.queryParams.dateInfo = this.$route.query.dateInfo;
      listCountPvInfo(this.queryParams).then(response => {
        this.countPvInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handle() {
      this.$refs.CountRef.show(this.queryParams)
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dateInfo: null,
        channelCode: null,
        channelName: null,
        appCode: null,
        appName: null,
        randomId: null,
        reqIp: null,
        browserName: null,
        platformName: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加渠道统计pv信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCountPvInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改渠道统计pv信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCountPvInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCountPvInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除渠道统计pv信息编号为"' + ids + '"的数据项？').then(function() {
        return delCountPvInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('channel/countPvInfo/export', {
        ...this.queryParams
      }, `countPvInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
